#ifndef _IM_SERVER_GROUP_DB_WRAPPER_H_
#define _IM_SERVER_GROUP_DB_WRAPPER_H_

#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include "database/simple_db_pool.h"
#include "base/singleton.h"
#include "base/config_reader.h"
#include "group_service_handler.h"

namespace imserver {

class GroupDBWrapper : public base::Singleton<GroupDBWrapper> {

public:
  static const int GROUP_MEMBER_DB_MOD;

  ~GroupDBWrapper() {
    if (conn_pool_) {
      delete conn_pool_;
    }
  }

  bool select_tb_group_id_seq(int64_t& seq);
  bool update_tb_group_id_seq();

  bool select_tb_group_info(int64_t group_id, int32_t type, int64_t entity_id, GroupInfo& group_info);
  bool update_tb_group_info(const GroupInfo& group_info);

  bool select_tb_group_member_m(int64_t group_id, int32_t status, std::set<int64_t>& member_ids);
  bool select_tb_group_member_g(int64_t uid, int32_t status, std::set<int64_t>& group_ids);
  bool update_tb_group_member(int64_t group_id, int64_t uid, int32_t status);
  bool delete_tb_group_member(int64_t group_id, int64_t uid);

private:
  friend class base::Singleton<GroupDBWrapper>;

  GroupDBWrapper() {
    ConfigReader cfg("../conf/group_service.conf");
    std::string database = "DB_GROUP";
    std::string addr = cfg.Get("DB_GROUP", "addr");
    std::string user = cfg.Get("DB_GROUP", "user");
    std::string pwd = cfg.Get("DB_GROUP", "pwd");
    conn_pool_ = 
        //boost::shared_ptr<base::SimpleConnectionPool>(
        new base::SimpleConnectionPool(database.c_str(), addr.c_str(), user.c_str(), pwd.c_str());//);
  }

  //boost::shared_ptr<base::SimpleConnectionPool> conn_pool_;
  base::SimpleConnectionPool* conn_pool_;
};

}

#endif // _IM_SERVER_GROUP_DB_WRAPPER_H_
